package com.example.mail;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import com.example.ISelectable;
import com.example.MessageCodes;
import com.example.R;
import com.example.popup.Select;
import core.model.Game;
import core.util.Logger;

import java.util.List;

public class MailNewActivity extends Activity implements ISelectable {
    private String selectedRace;
    private Game game;
    private CharSequence[] races;
    private String selectedMailType;
    private boolean mailTypeBroadCast;
    private MailManager manager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        game = (Game) getIntent().getExtras().getSerializable(Game.GAME);
        selectedMailType = getIntent().getExtras().getString("selectedMailType");


        manager = new MailManager(this, game, handler);

        setContentView(R.layout.mailnewlayout);

        TextView content = (TextView) findViewById(R.id.mailNewContent);
        content.setText(R.string.system_signature);

        if ("broadcast".equalsIgnoreCase(selectedMailType)) {
            changeTo("All");
        }

        List<String> loadedRaces = (List<String>) this.getIntent().getExtras().getSerializable("races");
        races = new CharSequence[loadedRaces.size()];
        int index = 0;
        for (String race : loadedRaces) {
            races[index++] = race;
        }

        Select.race(races, this, this);
    }

    private void changeTo(String to) {
        TextView toView = (TextView) this.findViewById(R.id.mailNewTo);
        toView.setText(toView.getText() + to);
    }

    @Override
    public void onItemSelected(int type, int item) {
        selectedRace = races[item].toString();
        changeTo(selectedRace);
    }


    public Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MessageCodes.EXCEPTION:
                    Logger.debug("MailNewActivity", "EXCEPTION");
                    break;
                case MessageCodes.Mail.SENDING_COMPLETE:
                    Logger.debug("MailNewActivity", "Mail sending was complete without any problems.");
                    showNotification();
                    break;
            }

        }
    };

    private void showNotification() {
        com.example.popup.Alert.info(this, "Your message was sent", new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                back();
            }
        });
    }

    private void back() {
        finish();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        TextView contentView = (TextView) findViewById(R.id.mailNewContent);
        String content = contentView.getText().toString();

        switch (item.getItemId()) {
            case R.id.mailNewMenuSend:
                if (isMailTypePrivate()) {
                    manager.sendMessage(selectedRace, content);
                }

                if (isMailTypeBroadCast()) {
                    manager.sendBroadcast(content);
                }

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mailnewmenu, menu);
        return true;
    }

    public boolean isMailTypePrivate() {
        return "private".equalsIgnoreCase(selectedMailType);
    }


    public boolean isMailTypeBroadCast() {
        return "broadcast".equalsIgnoreCase(selectedMailType);
    }
}
